import { RoleEntity } from 'src/api/role/entities/role.entity';
import { SharedEntity } from 'src/api/shared/entities/shared.entity';
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { UserEntity } from './user.entity';

@Entity('user_role')
export class UserRoleEntity extends SharedEntity {
  // 用户id
  @Column({
    type: 'varchar',
    name: 'user_id',
    length: 36,
    comment: '用户id',
  })
  userId: string;

  @ManyToOne(() => UserEntity, (user) => user.userRoles, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'user_id', referencedColumnName: 'id' }])
  user: UserEntity;

  // 角色id
  @Column({
    type: 'varchar',
    name: 'role_id',
    length: 36,
    comment: '角色id',
  })
  roleId: string;

  @ManyToOne(() => RoleEntity, (role) => role.userRoles, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'role_id', referencedColumnName: 'id' }])
  role: RoleEntity;
}
